"use client"
import React, { useEffect, useState } from 'react';
import type { FormProps } from 'antd';
import { Button, Checkbox, Form, Input, Select } from 'antd';
import * as api from '../../../../../../utils/https'
import { useParams, useRouter } from 'next/navigation';
type FieldType = {
  name?: string;
  Customer?: string;
  stage?: string;
  bjnum?: string;
  price?: string;
  days?: string
};

export default function Myform() {
  let { obj } = useParams()
  let router = useRouter()
  let [sss, setsss] = useState()
  const [form] = Form.useForm();
  let [title, settitle] = useState("")
  useEffect(() => {
    if (obj != "none") {
      settitle("编辑")
      api.http_table().then(res => {
        form.setFieldsValue((res.find(item => item.id == obj)))
        setsss(res.find(item => item.id == obj))
      }
      )
    } else {
      settitle("添加")

    }
  }, [])

  const onFinish: FormProps<FieldType>['onFinish'] = (values) => {
    console.log(values);
    let xiao = values.price
    let x = parseFloat(xiao).toFixed(2);
    let sj = new Date().getTime()
    if (title == "添加") {
      let obj = {
        id: sj,
        key: sj,
        ...values,
        price: x,
        time: sj,
        days: 30
      }
      api.http_addtable(obj)
    } else {
      let obj = {
        ...sss,
        ...values
      }
      api.http_edittable(obj)
    }
    router.push("/fff/dingdan/allding")

  };

  return (
    <div>
      <h2>{title}</h2>
      <Form
        name="basic"
        onFinish={onFinish}
        form={form}
      >
        <Form.Item<FieldType>
          label="订单标题"
          name="name"
          rules={[{ required: true, message: '订单标题不能为空!' }]}
        >
          <Input />
        </Form.Item>

        <Form.Item<FieldType>
          label="关联客户"
          name="Customer"
          rules={[{ required: true, message: '关联客户不能为空!' }]}
        >
          <Input />
        </Form.Item>
        <Form.Item<FieldType>
          label="订单阶段"
          name="stage"
          rules={[{ required: true, message: '订单阶段不能为空!' }]}
        >
          <Select
            showSearch
            placeholder="订单阶段"
            filterOption={(input, option) =>
              (option?.label ?? '').toLowerCase().includes(input.toLowerCase())
            }
            options={[
              { value: '初步洽谈', label: '初步洽谈' },
              { value: '未谈拢', label: '未谈拢' },
              { value: '项目完成', label: '项目完成' },
            ]}
          />

        </Form.Item>
        <Form.Item<FieldType>
          label="报价记录"
          name="bjnum"
          rules={[{ required: true, message: '报价记录不能为空!' }]}
        >
          <Input />
        </Form.Item>
        <Form.Item<FieldType>
          label="报价总金额"
          name="price"
          rules={[{ required: true, message: '报价总金额不能为空!' }]}
        >
          <Input />
        </Form.Item>
        <Form.Item<FieldType>
          label="未跟进天数"
          name="days"
          rules={[{ required: true, message: '未跟进天数不能为空!' }]}
        >
          <Input />
        </Form.Item>
        <Form.Item label={null}>
          <Button type="primary" htmlType="submit">
            确定
          </Button>
        </Form.Item>
      </Form>
    </div>
  )
}
